﻿package com.firewolf.state.context;

import com.firewolf.state.states.HasMoneyState;
import com.firewolf.state.states.IState;
import com.firewolf.state.states.NoMoneyState;
import com.firewolf.state.states.SoldOutState;
import com.firewolf.state.states.SoldState;

/**
 * 糖果机
 * @author Administrator
 *
 */
public class GumballMachine {
	
	private IState soldState;   //把所有的状态放在这里
	private IState soldOutState;
	private IState noMoneyState;
	private IState hasMoneyState;
	private IState state = soldOutState; //记录当前的状态 
	private int count = 0; //糖果数量，开始的时候为0
	
	public GumballMachine(int count){
		this.count = count;
		soldState = new SoldState(this);
		soldOutState = new SoldOutState(this);
		noMoneyState = new NoMoneyState(this);
		hasMoneyState = new HasMoneyState(this);
		if(count > 0){
			state = noMoneyState ; //如果超过0颗糖果，我们就把状态设置成NoMoneyState
		}
	}
	
	/**
	 * 投入25分钱
	 */
	public void insertMoney(){ 
		state.insert25tMoney();
	}
	
	/**
	 * 退钱
	 */
	public void ejectMoney(){
		state.ejectMoney();
	}
	
	/**
	 * 转动曲柄
	 */
	public void turnCrank(){
		state.turnCrank();
		state.dispense();
	}
	
	/**
	 * 设置状态
	 */
	public void setState(IState state){
		this.state = state ; 
	}
	
	/**
	 * 发放糖果
	 */
	public void releaseBall(){
		System.out.println("给你发放了一颗糖果----");
		if(count != 0 )
			count = count-1;
	}

	public IState getSoldState() {
		return soldState;
	}

	public IState getSoldOutState() {
		return soldOutState;
	}

	public IState getNoMoneyState() {
		return noMoneyState;
	}

	public IState getHasMoneyState() {
		return hasMoneyState;
	}

	public IState getState() {
		return state;
	}

	public int getCount() {
		return count;
	}
	
}
